package com.start.demo.account.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.start.demo.account.model.Account;
import com.start.demo.account.service.AccountService;
import com.start.demo.commons.Result;

@Controller
@RequestMapping("/account")
public class AccountController {

	@Autowired
	private AccountService accountService;

	/**
	 * 根据name查询
	 * 
	 * @param name
	 * @return
	 */
	// @RequestMapping(value = "/get/{name}", produces = {
	// "application/json;charset=UTF-8" })
	// @ResponseBody
	// public Account getAccountByName(@PathVariable("name") String name) {
	//
	// if (StringUtils.isBlank(name)) {
	// return null;
	// }
	// return accountService.getAccountByName(name);
	// }

	/**
	 * 根据id 查询
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/get/{id}", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public Account getAccountById(@PathVariable("id") Integer id) {

		if (null == id) {
			return null;
		}
		return accountService.getAccountById(id);
	}

	/**
	 * 
	 * @param account
	 * @return
	 */
	@RequestMapping(value = "/update", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String updateAccountById(Account account) {

		if (null == account) {
			Result result = new Result(0, "对象不能为空", "");
			return JSON.toJSONString(result);
		}

		accountService.updateAccountById(account);
		Result result = new Result(0, "修改成功", "");
		return JSON.toJSONString(result);
	}
}
